home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / EXTENSIONCLASS.REF < prev    next >
Encoding:
Text File  |  2000-02-16  |  33.0 KB  |  772 lines

  1. <h1>Extension Classes, Python Extension Types Become Classes</h1>
  2. <p>  Jim Fulton, Digital Creations, Inc.
  3.   jim@digicool.com</p>
  4.  
  5. <p>  <a href="COPYRIGHT.html">Copyright (C) 1996-1998, Digital Creations</a>.</p>
  6.  
  7. <h2>Abstract</h2>
  8. <p>    A lightweight mechanism has been developed for making Python
  9.     extension types more class-like.  Classes can be developed in an
  10.     extension language, such as C or C++, and these classes can be
  11.     treated like other python classes:</p>
  12.  
  13. <ul><li><p>They can be sub-classed in python,</p>
  14.  
  15.  
  16. <li><p>They provide access to method documentation strings, and</p>
  17.  
  18.  
  19. <li><p>They can be used to directly create new instances.</p>
  20.  
  21. </ul>
  22. <p>    An example class shows how extension classes are implemented and how
  23.     they differ from extension types.</p>
  24.  
  25. <p>    Extension classes provide additional extensions to class and
  26.     instance semantics, including:</p>
  27.  
  28. <ul><li><p>A protocol for accessing subobjects "in the context of" their
  29.       containers.  This is used to implement custom method types
  30.       and <a href="Acquisition.html">environmental acquisition</a>.</p>
  31.  
  32.  
  33. <li><p>A protocol for overriding method call semantics.  This is used
  34.       to implement "synchonized" classes and could be used to
  35.       implement argument type checking.</p>
  36.  
  37.  
  38. <li><p>A protocol for class initialization that supports execution of a
  39.       special <code>__class_init__</code> method after a class has been
  40.       initialized.</p>
  41.  
  42. </ul>
  43. <p>    Extension classes illustrate how the Python class mechanism can be
  44.     extended and may provide a basis for improved or specialized class
  45.     models. </p>
  46.  
  47.  
  48. <h2>Releases</h2>
  49. <p>    To find out what's changed in this release,
  50.     see the <a href="release.html">release notes</a>.</p>
  51.  
  52.  
  53. <h2>Problem</h2>
  54. <p>    Currently, Python provides two ways of defining new kinds of objects:</p>
  55.  
  56. <ul><li><p>Python classes</p>
  57.  
  58.  
  59. <li><p>Extension types</p>
  60.  
  61. </ul>
  62. <p>    Each approach has it's strengths.  Extension types provide much greater
  63.     control to the programmer and, generally, better performance.  Because
  64.     extension types are written in C, the programmer has greater access to 
  65.     external resources. (Note that Python's use of the term type has
  66.     little to do with the notion of type as a formal specification.)</p>
  67.  
  68. <p>    Classes provide a higher level of abstraction and are generally much
  69.     easier to develop.  Classes provide full inheritance support, while
  70.     support for inheritance when developing extension types is very
  71.     limited. Classes provide run-time meta-data, such as method documentation
  72.     strings, that are useful for documentation and discovery.  Classes
  73.     act as factories for creating instances, while separate functions
  74.     must be provided to create instances of types.</p>
  75.  
  76. <p>    It would be useful to combine the features of the two approaches.  It 
  77.     would be useful to be able to have better support for inheritance for
  78.     types, or to be able to subclass from types in Python.  It would be
  79.     useful to be able to have class-like meta-data support for types and
  80.     the ability to construct instances directly from types.</p>
  81.  
  82. <p>    Our software is developed in Python.  When necessary, we convert
  83.     debugged Python routines and classes to C for improved
  84.     performance.  In most cases, a small number of methods in a class
  85.     is responsible for most of the computation.  It should be possible
  86.     to convert only these methods to C, while leaving the other method
  87.     in Python.  A natural way to approach this is to create a base
  88.     class in C that contains only the performance-critical aspects of
  89.     a class' implementation and mix this base class into a Python
  90.     class. </p>
  91.  
  92. <p>    We have need, in a number of projects, for semantics that are
  93.     slightly different than the usual class and instance semantics,
  94.     yet we don't want to do most of our development in C.  For
  95.     example, we have developed a persistence mechanism <a href="#1">[1]</a> that
  96.     redefines <code>__getattr__</code> and <code>__setattr__</code> to take storage-related
  97.     actions when object state is accessed or modified.  We want to be
  98.     able to take certain actions on <em>every</em> attribute reference, but
  99.     for python class instances, <code>__getattr__</code> is only called when
  100.     attribute lookup fails by normal means.</p>
  101.  
  102. <p>    As another example, we would like to have greater control over how
  103.     methods are bound.  Currently, when accessing a class
  104.     instance attribute, the attribute value is bound together with the
  105.     instance in a method object <em>if and only if</em> the attribute value is a
  106.     python function.  For some applications, we might also want to be
  107.     able to bind extension functions, or other types of callable
  108.     objects, such as HTML document templates <a href="#2">[2]</a>. Furthermore,
  109.     we might want to have greater control over how objects are bound.
  110.     For example, we might want to bind instances and callable objects
  111.     with special method objects that assure that no more than one thread
  112.     accesses the object or method at one time.</p>
  113.  
  114. <p>    We can provide these special semantics in extension types, but we
  115.     wish to provide them for classes developed in Python.</p>
  116.  
  117.  
  118. <h2>Background</h2>
  119. <p>    At the first Python Workshop, Don Beaudry presented work <a href="#3">[3]</a> done
  120.     at V.I. Corp to integrate Python with C++ frameworks.  This system
  121.     provided a number of important features, including:</p>
  122.  
  123. <ul><li><p>Definition of extension types that provide class-like meta-data
  124.       and that can be called to create instances.</p>
  125.  
  126.  
  127. <li><p>Ability to subclass in python from C types.</p>
  128.  
  129.  
  130. <li><p>Ability to define classes in python who's data are stored as
  131.       C structures rather than in dictionaries to better interface to
  132.       C and C++ libraries, and for better performance.</p>
  133.  
  134.  
  135. <li><p>Less dynamic data structures.  In particular, the data structure
  136.       for a class is declared during class definition.</p>
  137.  
  138.  
  139. <li><p>Support for enumeration types.</p>
  140.  
  141. </ul>
  142. <p>    This work was not released, initially.</p>
  143.  
  144. <p>    Shortly after the workshop, changes were made to Python to support
  145.     the sub-classing features described in <a href="#3">[3]</a>.  These changes were not
  146.     documented until the fourth Python Workshop <a href="#4">[4]</a>.</p>
  147.  
  148. <p>    At the third Python workshop, I presented some work I had done on
  149.     generating module documentation for extension types.  Based on the
  150.     discussion at this workshop, I developed a meta-type proposal <a href="#5">[5]</a>.
  151.     This meta-type proposal was for an object that simply stored
  152.     meta-information for a type, for the purpose of generating module
  153.     documentation.</p>
  154.  
  155. <p>    In the summer of 1996, Don Beaudry released the system described in
  156.     <a href="#3">[3]</a> under the name MESS <a href="#6">[6]</a>. MESS addresses a number of needs but
  157.     has a few drawbacks:</p>
  158.  
  159. <ul><li><p>Only single inheritance is supported.</p>
  160.  
  161.  
  162. <li><p>The mechanisms for defining MESS extension types is very different
  163.       from and more complicated than the standard Python type creation
  164.       mechanism.</p>
  165.  
  166.  
  167. <li><p>Defining MESS types requires the use of an extensive C
  168.       applications programming interface.  This presents problems for
  169.       configuring dynamically-loaded extension modules unless the MESS
  170.       library is linked into the Python interpreter.</p>
  171.  
  172.  
  173. <li><p>Because the system tries to do a number of different things, it is
  174.       fairly large, about 15,000 lines.</p>
  175.  
  176.  
  177. <li><p>There is very little documentation, especially for the C
  178.       programming interface.</p>
  179.  
  180.  
  181. <li><p>The system is a work in progress, with a number of outstanding
  182.       bugs.</p>
  183.  
  184. </ul>
  185. <p>    As MESS matures, we expect most of these problems to be addressed.</p>
  186.  
  187.  
  188. <h2>Extension Classes</h2>
  189. <p>    To meet short term needs for a C-based persistence mechanism <a href="#1">[1]</a>, an
  190.     extension class module was developed using the mechanism described
  191.     in <a href="#4">[4]</a> and building on ideas from MESS <a href="#6">[6]</a>.  The extension class module
  192.     recasts extension types as "extension classes" by seeking to
  193.     eliminate, or at least reduce semantic differences between types and
  194.     classes. The module was designed to meet the following goal:</p>
  195.  
  196. <ul><li><p>Provide class-like behavior for extension types, including
  197.       interfaces for meta information and for constructing instances.</p>
  198.  
  199.  
  200. <li><p>Support sub-classing in Python from extension classes, with support
  201.       for multiple inheritance.</p>
  202.  
  203.  
  204. <li><p>Provide a small hardened implementation that can be used for
  205.       current products.</p>
  206.  
  207.  
  208. <li><p>Provide a mechanism that requires minimal modification to existing
  209.       extension types.</p>
  210.  
  211.  
  212. <li><p>Provide a basis for research on alternative semantics for classes
  213.       and inheritance.</p>
  214.  
  215. </ul>
  216. <p>    <strong>Note:</strong> I use <em>non-standard</em> terminology here.  By standard
  217.     <em>python</em> terminology, only standard python classes can be called
  218.     classes.  ExtensionClass "classes" are technically just "types"
  219.     that happen to swim, walk and quack like python classes.</p>
  220.  
  221. <h3>Base extension classes and extension subclasses</h3>
  222. <p>      Base extension classes are implemented in C.  Extension subclasses
  223.       are implemented in Python and inherit, directly or indirectly from
  224.       one or more base extension classes.  An extension subclass may
  225.       inherit from base extension classes, extension subclasses, and
  226.       ordinary python classes.  The usual inheritance order rules
  227.       apply.  Currently, extension subclasses must conform to the
  228.       following two rules:</p>
  229.  
  230. <ul><li><p>The first super class listed in the class statement defining an
  231.         extension subclass must be either a base extension class or an
  232.         extension subclass.  This restriction will be removed in
  233.         Python-1.5.</p>
  234.  
  235.  
  236. <li><p>At most one base extension direct or indirect super class may
  237.         define C data members.  If an extension subclass inherits from
  238.         multiple base extension classes, then all but one must be mix-in
  239.         classes that provide extension methods but no data.</p>
  240.  
  241. </ul>
  242.  
  243. <h3>Meta Information</h3>
  244. <p>      Like standard python classes, extension classes have the following
  245.       attributes containing meta-data:</p>
  246.  
  247. <dl><dt>      <code>__doc__</code>  <dd><p>a documentation string for the class,</p>
  248.  
  249.  
  250. <dt>      <code>__name__</code> <dd><p>the class name,</p>
  251.  
  252.  
  253. <dt>      <code>__bases__</code><dd><p>a sequence of base classes,</p>
  254.  
  255.  
  256. <dt>      <code>__dict__</code> <dd><p>a class dictionary, and</p>
  257.  
  258.  
  259. <dt>      <code>__module__</code><dd><p>the name of the module in which the class was
  260.                       defined. </p>
  261.  
  262. </dl>
  263. <p>      The class dictionary provides access to unbound methods and their
  264.       documentation strings, including extension methods and special
  265.       methods, such as methods that implement sequence and numeric
  266.       protocols.  Unbound methods can be called with instance first
  267.       arguments.</p>
  268.  
  269.  
  270. <h3>Subclass instance data</h3>
  271. <p>      Extension subclass instances have instance dictionaries, just
  272.       like Python class instances do.  When fetching attribute values,
  273.       extension class instances will first try to obtain data from the
  274.       base extension class data structure, then from the instance
  275.       dictionary, then from the class dictionary, and finally from base
  276.       classes.  When setting attributes, extension classes first attempt
  277.       to use extension base class attribute setting operations, and if
  278.       these fail, then data are placed in the instance dictionary.</p>
  279.  
  280.  
  281.  
  282. <h2>Implementing base extension classes</h2>
  283. <p>    A base extension class is implemented in much the same way that an
  284.     extension type is implemented, except:</p>
  285.  
  286. <ul><li><p>The include file, <code>ExtensionClass.h</code>, must be included.</p>
  287.  
  288.  
  289. <li><p>The type structure is declared to be of type <code>PyExtensionClass</code>, rather 
  290.       than of type <code>PyTypeObject</code>.</p>
  291.  
  292.  
  293. <li><p>The type structure has an additional member that must be defined
  294.       after the documentation string.  This extra member is a method chain
  295.       (<code>PyMethodChain</code>) containing a linked list of method definition
  296.       (<code>PyMethodDef</code>) lists.  Method chains can be used to implement
  297.       method inheritance in C.  Most extensions don't use method chains,
  298.       but simply define method lists, which are null-terminated arrays
  299.       of method definitions.  A macro, <code>METHOD_CHAIN</code> is defined in
  300.       <code>ExtensionClass.h</code> that converts a method list to a method chain.
  301.       (See the example below.)</p>
  302.  
  303.  
  304. <li><p>Module functions that create new instances must be replaced by 
  305.       <code>__init__</code> methods that initialize, but does not create storage for 
  306.       instances.</p>
  307.  
  308.  
  309. <li><p>The extension class must be initialized and exported to the module
  310.       with::</p>
  311. <PRE>
  312.           PyExtensionClass_Export(d,"name",type);
  313.  
  314.       where 'name' is the module name and 'type' is the extension class
  315.       type object.
  316.  
  317. </PRE>
  318.  
  319. </ul>
  320. <h3>Attribute lookup</h3>
  321. <p>      Attribute lookup is performed by calling the base extension class
  322.       <code>getattr</code> operation for the base extension class that includes C
  323.       data, or for the first base extension class, if none of the base
  324.       extension classes include C data.  <code>ExtensionClass.h</code> defines a
  325.       macro <code>Py_FindAttrString</code> that can be used to find an object's
  326.       attributes that are stored in the object's instance dictionary or
  327.       in the object's class or base classes:</p>
  328. <PRE>
  329.          v = Py_FindAttrString(self,name);
  330.  
  331. </PRE>
  332.  
  333. <p>      where <code>name</code> is a C string containing the attribute name.</p>
  334.  
  335. <p>      In addition, a macro is provided that replaces <code>Py_FindMethod</code>
  336.       calls with logic to perform the same sort of lookup that is
  337.       provided by <code>Py_FindAttrString</code>.</p>
  338.  
  339. <p>      If an attribute name is contained in a Python string object,
  340.       rather than a C string object, then the macro <code>Py_FindAttr</code> should
  341.       be used to look up an attribute value.</p>
  342.  
  343.  
  344. <h3>Linking</h3>
  345. <p>      The extension class mechanism was designed to be useful with
  346.       dynamically linked extension modules.  Modules that implement
  347.       extension classes do not have to be linked against an extension
  348.       class library.  The macro <code>PyExtensionClass_Export</code> imports the
  349.       <code>ExtensionClass</code> module and uses objects imported from this module
  350.       to initialize an extension class with necessary behavior.</p>
  351.  
  352.  
  353. <h3>Example: MultiMapping objects</h3>
  354. <p>      An <a href="MultiMapping.html">example</a> is provided that illustrates the
  355.       changes needed to convert an existing type to an ExtensionClass.</p>
  356.  
  357.  
  358.  
  359. <h2>Implementing base extension class constructors</h2>
  360. <p>    Some care should be taken when implementing or overriding base
  361.     class constructors.  When a Python class overrides a base class
  362.     constructor and fails to call the base class constructor, a
  363.     program using the class may fail, but it will not crash the
  364.     interpreter. On the other hand, an extension subclass that
  365.     overrides a constructor in an extension base class must call the
  366.     extension base class constructor or risk crashing the interpreter.
  367.     This is because the base class constructor may set C pointers that,
  368.     if not set properly, will cause the interpreter to crash when
  369.     accessed.  This is the case with the <code>MultiMapping</code> extension base
  370.     class shown in the example above.</p>
  371.  
  372. <p>    If no base class constructor is provided, extension class instance
  373.     memory will be initialized to 0.  It is a good idea to design
  374.     extension base classes so that instance methods check for
  375.     uninitialized memory and perform initialialization if necessary.
  376.     This was not done above to simplify the example.</p>
  377.  
  378.  
  379. <h2>Overriding methods inherited from Python base classes</h2>
  380. <p>    A problem occurs when trying to overide methods inherited from
  381.     Python base classes.  Consider the following example:</p>
  382. <PRE>
  383.       from ExtensionClass import Base
  384.  
  385.       class Spam:
  386.  
  387.         def __init__(self, name):
  388.           self.name=name
  389.  
  390.       class ECSpam(Base, Spam):
  391.  
  392.         def __init__(self, name, favorite_color):
  393.           Spam.__init__(self,name)
  394.           self.favorite_color=favorite_color
  395.  
  396. </PRE>
  397.  
  398. <p>    This implementation will fail when an <code>ECSpam</code> object is
  399.     instantiated.  The problem is that <code>ECSpam.__init__</code> calls
  400.     <code>Spam.__init__</code>, and <code>Spam.__init__</code> can only be called with a
  401.     Python instance (an object of type <code>"instance"</code>) as the first
  402.     argument.  The first argument passed to <code>Spam.__init__</code> will be an
  403.     <code>ECSpam</code> instance (an object of type <code>ECSPam</code>).</p>
  404.  
  405. <p>    To overcome this problem, extension classes provide a class method
  406.     <code>inheritedAttribute</code> that can be used to obtain an inherited
  407.     attribute that is suitable for calling with an extension class
  408.     instance.  Using the <code>inheritedAttribute</code> method, the above
  409.     example can be rewritten as:</p>
  410. <PRE>
  411.       from ExtensionClass import Base
  412.  
  413.       class Spam:
  414.  
  415.         def __init__(self, name):
  416.           self.name=name
  417.  
  418.       class ECSpam(Base, Spam):
  419.  
  420.         def __init__(self, name, favorite_color):
  421.           ECSpam.inheritedAttribute('__init__')(self,name)
  422.           self.favorite_color=favorite_color
  423.  
  424. </PRE>
  425.  
  426. <p>    This isn't as pretty but does provide the desired result.</p>
  427.  
  428.  
  429. <h2>New class and instance semantics</h2>
  430. <h3>Context Wrapping</h3>
  431. <p>      It is sometimes useful to be able to wrap up an object together
  432.       with a containing object.  I call this "context wrapping"
  433.       because an object is accessed in the context of the object it is
  434.       accessed through.</p>
  435.  
  436. <p>      We have found many applications for this, including:</p>
  437. <ul><li><p>User-defined method objects,</p>
  438.  
  439.  
  440. <li><p><a href="Acquisition.html">Acquisition</a> and</p>
  441.  
  442.  
  443. <li><p>Computed attributes</p>
  444.  
  445. </ul>
  446.  
  447. <h4>User-defined method objects</h4>
  448. <p>        Python classes wrap Python function attributes into methods.  When a
  449.         class has a function attribute that is accessed as an instance
  450.         attribute, a method object is created and returned that contains
  451.         references to the original function and instance.  When the method
  452.         is called, the original function is called with the instance as the
  453.         first argument followed by any arguments passed to the method.</p>
  454.  
  455. <p>        Extension classes provide a similar mechanism for attributes that
  456.         are Python functions or inherited extension functions.  In
  457.         addition, if an extension class attribute is an instance of an
  458.         extension class that defines an <code>__of__</code> method, then when the
  459.         attribute is accessed through an instance, it's <code>__of__</code> method
  460.         will be called to create a bound method.</p>
  461.  
  462. <p>        Consider the following example:</p>
  463. <PRE>
  464.           import ExtensionClass
  465.  
  466.           class CustomMethod(ExtensionClass.Base):
  467.  
  468.             def __call__(self,ob): 
  469.               print 'a %s was called' % ob.__class__.__name__
  470.  
  471.             class wrapper:
  472.  
  473.               def __init__(self,m,o): self.meth, self.ob=m,o
  474.  
  475.               def __call__(self): self.meth(self.ob)
  476.  
  477.             def __of__(self,o): return self.wrapper(self,o)
  478.  
  479.           class bar(ExtensionClass.Base):
  480.             hi=CustomMethod()
  481.  
  482.           x=bar()
  483.           hi=x.hi()
  484.  
  485. </PRE>
  486.  
  487. <p>        Note that <code>ExtensionClass.Base</code> is a base extension class that
  488.         provides very basic ExtensionClass behavior. </p>
  489.  
  490. <p>        When run, this program outputs: <code>a bar was called</code>.</p>
  491.  
  492.  
  493. <h4>Computed Attributes</h4>
  494. <p>        It is not uncommon to wish to expose information via the
  495.         attribute interface without affecting implementation data
  496.         structures.  One can use a custom <code>__getattr__</code> method to
  497.         implement computed attributes, however, this can be a bit
  498.         cumbersome and can interfere with other uses of <code>__getattr__</code>,
  499.         such as for persistence.</p>
  500.  
  501. <p>        The <code>__of__</code> protocol provides a convenient way to implement
  502.         computed attributes. First, we define a ComputedAttribute
  503.         class.  a ComputedAttribute is constructed with a function to
  504.         be used to compute an attribute, and calls the function when
  505.         it's <code>__of__</code> method is called:</p>
  506. <p>          import ExtensionClass</p>
  507.  
  508. <p>          class ComputedAttribute(ExtensionClass.Base):</p>
  509. <p>            def __init__(self, func): self.func=func</p>
  510.  
  511. <p>            def __of__(self, parent): return self.func(parent)</p>
  512.  
  513.  
  514.  
  515. <p>        Then we can use this class to create computed attributes.  In the
  516.         example below, we create a computed attribute, <code>radius</code>:</p>
  517. <p>          from math import sqrt</p>
  518.  
  519. <p>          class Point(ExtensionClass.Base):</p>
  520. <p>            def __init__(self, x, y): self.x, self.y = x, y</p>
  521.  
  522. <p>            radius=ComputedAttribute(lambda self: sqrt(self.x**2+self.y**2))</p>
  523.  
  524.  
  525.  
  526. <p>        which we can use just like an ordinary attribute:</p>
  527. <p>          p=Point(2,2)
  528.           print p.radius</p>
  529.  
  530.  
  531.  
  532.  
  533. <h3>Overriding method calls</h3>
  534. <p>      Normally, when a method is called, the function wrapped by the
  535.       method is called directly by the method.  In some cases, it is
  536.       useful for user-defined logic to participate in the actual
  537.       function call.  Extension classes introduce a new protocol that
  538.       provides extension classes greater control over how their
  539.       methods are called.  If an extension class defines a special
  540.       method, <code>__call_method__</code>, then this method will be called to
  541.       call the functions (or other callable object) wrapped by the
  542.       method.  The method. <code>__call_method__</code> should provide the same
  543.       interface as provided by the Python builtin <code>apply</code> function.</p>
  544.  
  545. <p>      For example, consider the expression: <code>x.meth(arg1, arg2)</code>.  The
  546.       expression is evaluated by first computing a method object that
  547.       wraps <code>x</code> and the attribute of <code>x</code> stored under the name <code>meth</code>.
  548.       Assuming that <code>x</code> has a <code>__call_method__</code> method defined, then
  549.       the <code>__call_method__</code> method of <code>x</code> will be called with two
  550.       arguments, the attribute of <code>x</code> stored under the name <code>meth</code>,
  551.       and a tuple containing <code>x</code>, <code>arg1</code>, and <code>arg2</code>.</p>
  552.  
  553. <p>      To see how this feature may be used, see the Python module,
  554.       <code>Syn.py</code>, which is included in the ExtensionClass distribution.
  555.       This module provides a mix-in class that provides Java-like
  556.       "synchonized" classes that limit access to their methods to one
  557.       thread at a time.</p>
  558.  
  559. <p>      An interesting application of this mechanism would be to
  560.       implement interface checking on method calls.</p>
  561.  
  562.  
  563. <h3>Method attributes</h3>
  564. <p>      Methods of ExtensionClass instances can have user-defined
  565.       attributes, which are stored in their associated instances.</p>
  566.  
  567. <p>      For example:</p>
  568. <PRE>
  569.         class C(ExtensionClass.Base):
  570.  
  571.           def get_secret(self):
  572.             "Get a secret"
  573.             ....
  574.  
  575.         c=C()
  576.  
  577.         c.f.__roles__=['Trusted People']
  578.  
  579.         print c.f.__roles__ # outputs ['Trusted People']
  580.         print c.f__roles__  # outputs ['Trusted People']
  581.  
  582.         print C.f.__roles__ # fails, unbound method
  583.  
  584. </PRE>
  585.  
  586. <p>      A bound method attribute is set by setting an attribute in it's
  587.       instance with a name consisting of the concatination of the
  588.       method's <code>__name__</code> attribute and the attribute name.
  589.       Attributes cannot be set on unbound methods.</p>
  590.  
  591.  
  592. <h3>Class initialization</h3>
  593. <p>      Normal Python class initialization is similar to but subtley
  594.       different from instance initialization.  An instance <code>__init__</code>
  595.       function is called on an instance immediately <em>after</em> it is
  596.       created.  An instance <code>__init__</code> function can use instance
  597.       information, like it's class and can pass the instance to other
  598.       functions.  On the other hand, the code in class statements is
  599.       executed immediately <em>before</em> the class is created.  This means
  600.       that the code in a class statement cannot use class attributes,
  601.       like <code>__bases__</code>, or pass the class to functions.</p>
  602.  
  603. <p>      Extension classes provide a mechanism for specifying code to be
  604.       run <em>after</em> a class has been created.  If a class or one of it's
  605.       base classes defines a <code>__class_init__</code> method, then this method
  606.       will be called just after a class has been created.  The one
  607.       argument passed to the method will be the class, <em>not</em> an
  608.       instance of the class.</p>
  609.  
  610.  
  611.  
  612. <h2>Useful macros defined in ExtensionClass.h</h2>
  613. <p>    A number of useful macros are defined in ExtensionClass.h.
  614.     These are documented in <code>ExtensionClass.h</code>.</p>
  615.  
  616.  
  617. <h2>Pickleability</h2>
  618. <p>    Classes created with ExtensionClass, including extension base
  619.     classes are automatically pickleable.  The usual gymnastics
  620.     necessary to pickle <code>non-standard</code> types are not necessray for
  621.     types that have been modified to be extension base classes.</p>
  622.  
  623.  
  624. <h2>Status</h2>
  625. <p>    The current release of the extension class module is <a href="http://www.digicool.com/releases/ExtensionClass/ExtensionClass-1.1.tar.gz">1.1</a>.
  626.     The core implementation has less than four thousand lines of code,
  627.     including comments.  This release requires Python 1.4 or higher.</p>
  628.  
  629. <p>    To find out what's changed in this release, see the
  630.     <a href="release.html">release notes</a>.</p>
  631.  
  632. <p>    <a href="Installation.html">Installation instructions</a> are provided.</p>
  633.  
  634.  
  635. <h2>Issues</h2>
  636. <p>    There are a number of issues that came up in the course of this work
  637.     and that deserve mention.</p>
  638.  
  639. <ul><li><p>In Python 1.4, the class extension mechanism described in <a href="#4">[4]</a> required
  640.       that the first superclass in a list of super-classes must be of the
  641.       extended class type.  This may not be convenient if mix-in
  642.       behavior is desired.  If a list of base classes starts with a
  643.       standard python class, but includes an extension class, then an
  644.       error was raised.  It would be more useful if, when a list of base
  645.       classes contains one or more objects that are not python classes,
  646.       the first such object was used to control the extended class
  647.       definition.  To get around this, the <code>ExtensionClass</code> module exports
  648.       a base extension class, <code>Base</code>, that can be used as the first base
  649.       class in a list of base classes to assure that an extension
  650.       subclass is created.</p>
  651. <p>      Python 1.5 allows the class extension even if the first non-class
  652.       object in the list of base classes is not the first object in
  653.       the list.  This issue appears to go away in Python 1.5, however,
  654.       the restriction that the first non-class object in a list of
  655.       base classes must be the first in the list may reappear in later
  656.       versions of Python.</p>
  657.  
  658.  
  659.  
  660. <li><p>Currently, only one base extension class can define any data in
  661.       C.  The data layout of subclasses-instances is the same as for the
  662.       base class that defines data in C, except that the data structure
  663.       is extended to hold an instance dictionary.  The data structure
  664.       begins with a standard python header, and extension methods expect
  665.       the C instance data to occur immediately after the object header.  If
  666.       two or more base classes defined C data, the methods for the
  667.       different base classes would expect their data to be in the same
  668.       location. A solution might be to allocate base class instances and
  669.       store pointers to these instances in the subclass data structure.
  670.       The method binding mechanism would have to be a more complicated
  671.       to make sure that methods were bound to the correct base data
  672.       structure.  Alternatively, the signature of C methods could be
  673.       expanded to allow pointers to expected class data to be passed
  674.       in addition to object pointers.</p>
  675.  
  676.  
  677. <li><p>There is currently no support for sub-classing in C, beyond that
  678.       provided by method chains.</p>
  679.  
  680.  
  681. <li><p>Rules for mixed-type arithmetic are different for python class
  682.       instances than they are for extension type instances.  Python
  683.       classes can define right and left versions of numeric binary
  684.       operators, or they can define a coercion operator for converting
  685.       binary operator operands to a common type.  For extension types,
  686.       only the latter, coercion-based, approach is supported.  The
  687.       coercion-based approach does not work well for many data types for
  688.       which coercion rules depend on the operator.  Because extension
  689.       classes are based on extension types, they are currently limited
  690.       to the coercion-based approach.  It should be possible to
  691.       extend the extension class implementation to allow both types of
  692.       mixed-type arithmetic control.</p>
  693.  
  694.  
  695. <li><p>I considered making extension classes immutable, meaning that
  696.       class attributes could not be set after class creation.  I also
  697.       considered making extension subclasses cache inherited
  698.       attributes.  Both of these are related and attractive for some
  699.       applications, however, I decided that it would be better to retain
  700.       standard class instance semantics and provide these features as
  701.       options at a later time.</p>
  702.  
  703.  
  704. <li><p>The extension class module defines new method types to bind C and
  705.       python methods to extension class instances.  It would be useful
  706.       for these method objects to provide access to function call
  707.       information, such as the number and names of arguments and the
  708.       number of defaults, by parsing extension function documentation
  709.       strings.</p>
  710.  
  711. </ul>
  712.  
  713. <h2>Applications</h2>
  714. <p>    Aside from test and demonstration applications, the extension class
  715.     mechanism has been used to provide an extension-based implementation
  716.     of the persistence mechanism described in <a href="#1">[1]</a>.  We have developed
  717.     this further to provide features such as automatic deactivation of
  718.     objects not used after some period of time and to provide more
  719.     efficient persistent-object cache management.</p>
  720.  
  721. <p>    Acquisition has been heavily used in our recent products.
  722.     Synchonized classes have also been used in recent products.</p>
  723.  
  724.  
  725. <h2>Summary</h2>
  726. <p>    The extension-class mechanism described here provides a way to add
  727.     class services to extension types.  It allows:</p>
  728. <ul><li><p>Sub-classing extension classes in Python,</p>
  729.  
  730.  
  731. <li><p>Construction of extension class instances by calling extension
  732.         classes,</p>
  733.  
  734.  
  735. <li><p>Extension classes to provide meta-data, such as unbound methods
  736.         and their documentation string.</p>
  737.  
  738. </ul>
  739.  
  740. <p>    In addition, the extension class module provides a relatively
  741.     concise example of the use of mechanisms that were added to Python
  742.     to support MESS <a href="#6">[6]</a>, and that were described at the fourth Python
  743.     Workshop <a href="#4">[4]</a>.  It is hoped that this will spur research in improved
  744.     and specialized models for class implementation in Python.</p>
  745.  
  746.  
  747. <p>  References</p>
  748.  
  749. <p>  <a name="1">[1]</a> Fulton, J., <a href="http://www.digicool.com/papers/Persistence.html">Providing Persistence for World-Wide-Web Applications</a>
  750.  Proceedings of the 5th Python Workshop.</p>
  751.  
  752. <p>  <a name="2">[2]</a> Page, R. and Cropper, S., <a href="http://www.digicool.com/papers/DocumentTemplate.html">Document Template</a>
  753.  Proceedings of the 5th Python Workshop.</p>
  754.  
  755. <p>  <a name="3">[3]</a> Beaudry, D., <a href="http://www.python.org/workshops/1994-11/BuiltInClasses/BuiltInClasses_1.html">Deriving Built-In Classes in Python</a>
  756.  Proceedings of the First International Python Workshop.</p>
  757.  
  758. <p>  <a name="4">[4]</a> Van Rossum, G., <a href="http://www.python.org/workshops/1996-06/notes/thursday.html">Don Beaudry Hack - MESS</a>
  759.  presented in the Developer's Future Enhancements session of the 
  760.  4th Python Workshop. </p>
  761.  
  762. <p>  <a name="5">[5]</a> Fulton, J., <a href="http://www.digicool.com/jim/MetaType.c">Meta-Type Object</a>
  763.  This is a small proposal, the text of which is contained in a 
  764.  sample implementation source file,  </p>
  765.  
  766. <p>  <a name="6">[6]</a> Beaudry, D., and Ascher, D., "The Meta-Extension Set", 
  767.  http://starship.skyport.net/~da/mess/.
  768. </p>
  769.  
  770.  
  771.  
  772.